package fr.notoriousmaster.fury.lib.gesture;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;

public class BoulisteScaleListener 
extends ScaleGestureDetector.SimpleOnScaleGestureListener {
	private ImageView imgCircle;
	
    private float mRadius = 150;
	private int px = 400;
	private int py = 240;
    private boolean bPinch = false;
        
    public BoulisteScaleListener(View view) {
    	imgCircle = (ImageView) view;
    	DrawCircle(px,py,mRadius,imgCircle);
    }
    
	@Override
    public boolean onScale(ScaleGestureDetector detector) {
    	mRadius*=detector.getScaleFactor();
        mRadius = Math.max(0.1f, Math.min(mRadius, 400.0f));
        DrawCircle(px,py,mRadius,imgCircle);
        
        return true;
    }
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
    	bPinch=true;
    	return true;
    }
    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
    	bPinch=false;
    }
    
	public boolean isPinching() {
		return bPinch;
	}
	
	public ImageView getImgCircle() {
		return imgCircle;
	}

	public void setImgSensor(ImageView imgCircle) {
		this.imgCircle = imgCircle;
	}
    
	public void setX(int x) {
		px=x;
	}
	public void setY(int y) {
		py=y;
	}
	
	public void ReDrawCircle () {
		DrawCircle(px,py,mRadius,imgCircle);
	}
    private void DrawCircle(float x, float y, float radius, ImageView image) {

        Bitmap bm=Bitmap.createBitmap(800,800,Config.ARGB_8888);
        
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        
        Canvas canvas = new Canvas(bm);
        
        try {
	        paint.setColor(Color.argb(80,222,231,239));
	        paint.setStyle(Paint.Style.FILL);
	        canvas.drawCircle(x, y, radius, paint);
	        
	        paint.setColor(Color.argb(255,0,0,255));
	        paint.setStyle(Paint.Style.STROKE);
	        paint.setStrokeWidth(1);
	        canvas.drawCircle(x, y, radius, paint);
	
	        paint.setColor(Color.argb(255,0,0,0));
	        paint.setStyle(Paint.Style.STROKE);
	        paint.setStrokeWidth(1);
	        canvas.drawCircle(x, y, 5, paint);
        }
        catch(Exception e) {
        }
        
        image.setImageBitmap(bm);
 	   
    }
    
}
